package com.drug.mingkang.controller;

import com.drug.mingkang.mapper.*;
import com.drug.mingkang.model.*;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.OrderReturnApplyService;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 退货订单表(OrderReturnApply)表控制层
 *
 * @author Mr.wang
 * @since 2020-08-11 12:00:14
 */
@RestController
@RequestMapping("orderReturnApply")
public class OrderReturnApplyController {
    /**
     * 服务对象
     */
    @Resource
    private OrderReturnApplyService orderReturnApplyService;
    @Resource
    private HqDrugtypeMapper hqDrugtypeMapper;
    @Resource
    private HqDrugMapper hqDrugMapper;
    @Resource
    private ImmRepertoryMapper immRepertoryMapper;
    @Resource
    private ReturnDetailMapper returnDetailMapper;
    @Resource
    private HqInventoryMapper hqInventoryMapper;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public OrderReturnApply selectOne(String id) {
        return this.orderReturnApplyService.queryById(id);
    }


    /**
     * 退款申请
     * @param jsonmes    需要退的数据
     * @param refundCause   退款原因
     */
    @GetMapping("refundPutIn")
    public void refundPutIn(@Param("refundCause") String jsonmes, @Param("refundCause") String refundCause){
        orderReturnApplyService.refundPutIn(jsonmes,refundCause);

    }


    //传入一个采购明细id
    @GetMapping("selectDrug")
    public Map<String,Object> selectDrug(String id, HttpSession session){
        String zozo = "";
        Set<String> set = new HashSet<>();
        List<ImmPurchaseDeatis> immPurchaseDeatis = orderReturnApplyService.listPurchase(id);
        for (ImmPurchaseDeatis immPurchaseDeatis1 : immPurchaseDeatis){
            //System.out.println(immPurchaseDeatis1.getDrugId()+"药品id"+immPurchaseDeatis1.getMoney()+"采购单价"+immPurchaseDeatis1.getCount()+"采购数量");
            List<HqDrugtype> drugType = hqDrugtypeMapper.drugType(immPurchaseDeatis1.getDrugId());
            for(HqDrugtype drugType1:drugType){
                //添加唯一的数据
                set.add(drugType1.getTypeId());
            }
        }

        //存放所有的
        List<Object> list = new ArrayList<>();
        for(String one:set){
            HqDrug hqDrug = null;
            //查询类型名称
            List<HqDrug> hqDrugs = hqDrugMapper.queryDrugTypeId1(one);
            for(HqDrug hqDrug1:hqDrugs){
                hqDrug = new HqDrug();
                hqDrug.setTypeName(hqDrug1.getTypeName());
                hqDrug.setTypeId(hqDrug1.getTypeId());
                hqDrug.setImages("http://localhost:8090/ok-admin/pages/orderRetrun/images/typeImg.png");
                //临时存放集合
                List<Object> list2 = new ArrayList<>();
                //通过订单好查询所有的药品
                for(ImmPurchaseDeatis immPurchaseDeatis1 : immPurchaseDeatis){
                    List<HqDrug> hqDrugs1 = hqDrugMapper.queryDrugTypeId(one,immPurchaseDeatis1.getDrugId());
                    for (HqDrug zizi:hqDrugs1){
                        HqDrug hqDrug2 = new HqDrug();
                        //药品id
                        hqDrug2.setDrugId(zizi.getDrugId());
                        //药品Name
                        hqDrug2.setDrugName(zizi.getDrugName());
                        //出库价
                        hqDrug2.setDrugOut(zizi.getDrugOut());
                        //采购数量
                        hqDrug2.setCount(immPurchaseDeatis1.getCount());
                        //images图片
                        hqDrug2.setImages(zizi.getImages());
                        //药品说明
                        hqDrug2.setDrugExp(zizi.getDrugExp());
                        //别名
                        hqDrug2.setAlias(zizi.getAlias());
                        //主要用药禁忌
                        hqDrug2.setTaboo(zizi.getTaboo());
                        ///药品类型id
                        hqDrug2.setTypeId(zizi.getTypeId());
                        //当前分公司id
                        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
                        String immId = perUsers.getCompanyId();
                        System.out.println(zizi.getDrugId()+"                      "+immId);
                        //获取库存数量
                        ImmRepertory immRepertory = immRepertoryMapper.queryDrugId(zizi.getDrugId(),immId);
                        //库存数量
                        hqDrug2.setRepertoryCount(immRepertory.getCount());
                        //采购订单id                                                           采购订单需要改活
                        hqDrug2.setPurchaseId(id);
                        list2.add(hqDrug2);
                        hqDrug.setTreeList(list2);
                    }

                }
            }
            list.add(hqDrug);
        }
        //显示list集合
        Gson gson = new Gson();
        String s = gson.toJson(list);
        System.out.println(s);
        //map集合
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        map.put("count",10);
        map.put("msg","");
        map.put("code","0");

        return map;
    }


    /**
     * 入库
     * @param id    订单id
     * @param
     */
    @GetMapping("financeDeposit")
    public void financeDeposit(@Param("id") String id,HttpSession session){
        //当前分公司id
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        String companyId = perUsers.getCompanyId();
        UUID uuid = UUID.randomUUID();
        long leastSignificantBits = uuid.getMostSignificantBits();
        System.out.println(leastSignificantBits);
        OrderReturnApply orderReturnApply = orderReturnApplyService.queryById(id);
        System.out.println("退货原因"+orderReturnApply.getReason()+"退货总价格"+orderReturnApply.getProductPrice()
                +"退货总数量"+orderReturnApply.getProductCount()+"申请状态"
                +orderReturnApply.getApplyState()+"分公司id"+orderReturnApply.getImmID());
        List<ReturnDetail> returnDetails = returnDetailMapper.queryOrderId(orderReturnApply.getOrderId());
        if(returnDetails.size()!=0){
            for (ReturnDetail re: returnDetails
                 ) {
                HqInventory hqInventory1 = hqInventoryMapper.queryByDrugId(id);
                if(hqInventory1!=null){
                    HqInventory hqInventory = new HqInventory();
                    hqInventory.setInventoryId(leastSignificantBits+"");
                    hqInventory.setDrugId(re.getDrug());
                    hqInventory.setInventoryNum(re.getCounts()+hqInventory1.getInventoryNum());
                    hqInventory.setIsva(1);
                    hqInventory.setOper("1");
                    hqInventoryMapper.update(hqInventory);
                }else{
                    HqInventory hqInventory = new HqInventory();
                    hqInventory.setInventoryId(leastSignificantBits+"");
                    hqInventory.setDrugId(re.getDrug());
                    hqInventory.setInventoryNum(re.getCounts());
                    hqInventory.setIsva(1);
                    hqInventory.setOper("1");
                    hqInventoryMapper.insert(hqInventory);
                }

                for (int i = 0; i <returnDetails.size() ; i++) {
                    ImmRepertory immRepertory = new ImmRepertory();
                    immRepertory.setRepertoryId(leastSignificantBits+"");
                    immRepertory.setImmId(orderReturnApply.getImmID());
                    immRepertory.setDrugId(returnDetails.get(i).getDrug());
                    ImmRepertory immRepertory1 = immRepertoryMapper.queryDrugId(returnDetails.get(i).getDrug(),companyId);
                    immRepertory.setCount(immRepertory1.getCount()-returnDetails.get(i).getCounts());
                    immRepertory.setOperator("1");
                    immRepertory.setIsva(1);
                    immRepertoryMapper.update(immRepertory);
                }
                OrderReturnApply orderReturnApply1 = new OrderReturnApply();
                orderReturnApply1.setOrderId(orderReturnApply.getOrderId());
                //退货入库  93
                orderReturnApply1.setApplyState(93);
                orderReturnApplyService.update(orderReturnApply1);
            }
        }
    }



}